package ca.polymtl.liom.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;

import ca.polymtl.liom.gui.LIOMStimulationListDisplay;
import ca.polymtl.liom.LIOMOCTExperiment;
import javax.swing.JToolBar;
import javax.swing.JToggleButton;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LIOMOCTMainGui {

	private JFrame frmOctAcquisition;
	private LIOMOCTExperiment p_oct_exp = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LIOMOCTExperiment oct_exp = new LIOMOCTExperiment();
					if( oct_exp.setExperimentFolder()){
						LIOMOCTMainGui window = new LIOMOCTMainGui(oct_exp);
						window.frmOctAcquisition.setVisible(true);
					}
					else
					{
						System.out.println("Experiment cancelled.");
					}
				} catch (Exception e) {
					// Fail safe 
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LIOMOCTMainGui(LIOMOCTExperiment oct_exp) {
		initialize();
		p_oct_exp = oct_exp;
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmOctAcquisition = new JFrame();
		frmOctAcquisition.setTitle("OCT Acquisition");
		frmOctAcquisition.setBounds(100, 100, 450, 300);
		frmOctAcquisition.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		frmOctAcquisition.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		JPanel p_image_panel = new JPanel();
		tabbedPane.addTab("Image", null, p_image_panel, null);
		
		JPanel p_galvo_panel = new JPanel();
		tabbedPane.addTab("Galvo Ramps", null, p_galvo_panel, null);
		
		JPanel p_aux_panel = new JPanel();
		tabbedPane.addTab("Aux", null, p_aux_panel, null);
		
		JPanel p_fileio_panel = new JPanel();
		tabbedPane.addTab("File IO", null, p_fileio_panel, null);
		
		LIOMStimulationListDisplay p_stim_panel = new LIOMStimulationListDisplay();
		tabbedPane.addTab("Stimulations", null, p_stim_panel, null);
		
		JToolBar toolBar = new JToolBar();
		frmOctAcquisition.getContentPane().add(toolBar, BorderLayout.NORTH);
		
		JButton btnRun = new JButton("Run");
		btnRun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				p_oct_exp.runAcquisition();
			}
		});
		btnRun.setForeground(Color.GREEN);
		toolBar.add(btnRun);
		
		JButton btnStop = new JButton("Stop");
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				p_oct_exp.stopAcquisition();
			}
		});
		btnStop.setForeground(Color.RED);
		toolBar.add(btnStop);
		
		JToggleButton tglbtnSave = new JToggleButton("Save");
		toolBar.add(tglbtnSave);
	}

}
